/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: FileController
 * Author:   周宏硕
 * Date:     2018/10/31 20:52
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.htwx.controller;

import com.htwx.entity.Manager;
import com.htwx.service.ManagerService;
import com.htwx.util.ExcelUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

/**
 * @author 周宏硕
 * @create 2018/10/31
 * @since 1.0.0
 */
@Controller
@RequestMapping("FileController")
public class FileController {

    @Autowired
    private ManagerService managerService;

    //上传Excel
    @RequestMapping("/uploadExcel")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
        List<Manager> list=managerService.selectAllManager(null);
        //excel标题
        String[] title = {"编号", "账号", "密码", "类别"};

        //excel文件名
        String fileName = "管理员表" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "管理员表";

        String content[][]=new  String[list.size()][];

        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            Manager obj = list.get(i);
            content[i][0] = String.valueOf(obj.getManagerId());
            content[i][1] = obj.getManagerName();
            content[i][2] = obj.getManagerPass();
            content[i][3] = String.valueOf(obj.getManagerType());

        }

//创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

//响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}